package src.java.easy;

public class Solution13 {
    public  static int romanToInt(String s) {
        char[] charArray = s.toCharArray();
        int result = 0;
        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] == 'I') {
                if (i + 1 < charArray.length && charArray[i + 1] == 'V') {
                    result += 4;
                    i++;
                }
                else if (i + 1 < charArray.length && charArray[i + 1] == 'X') {
                    result += 9;
                    i++;
                }
                else result += 1;
            } else if (charArray[i] == 'X') {
                if (i + 1 < charArray.length && charArray[i + 1] == 'L') {
                    result += 40;
                    i++;
                }
                else if (i + 1 < charArray.length && charArray[i + 1] == 'C') {
                    result += 90;
                    i++;
                }
                else result += 10;
            } else if (charArray[i] == 'C') {
                if (i + 1 < charArray.length && charArray[i + 1] == 'D') {
                    result += 400;
                    i++;
                }
                else if (i + 1 < charArray.length && charArray[i + 1] == 'M') {
                    result += 900;
                    i++;
                }
                else result += 100;
            } else if (charArray[i] == 'V') result += 5;
            else if (charArray[i] == 'L') result += 50;
            else if (charArray[i] == 'D') result += 500;
            else if (charArray[i] == 'M') result += 1000;
        }
        return result;
    }

    public static void main(String[] args) {
        String s = "MCMXCIV";
        System.out.println(romanToInt(s)); // Output: 1994
    }
}
